﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Persenter.ServiceReference1;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using System.IO;

namespace Persenter
{
    public partial class Page : UserControl
    {
        Storyboard looper;

        public Page()
        {
            InitializeComponent();
            looper = new Storyboard();
            looper.Duration = new Duration(TimeSpan.FromSeconds(5));
            looper.Completed += new EventHandler(looper_Completed);
            this.Loaded += new RoutedEventHandler(Page_Loaded);
            this.left.MouseLeftButtonDown += left_MouseLeftButtonDown;
            this.right.MouseLeftButtonDown += right_MouseLeftButtonDown;
        }

        void right_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sumer < counter)
            {
                sumer += 1;
            }
            showad(ads[sumer]);
            looper.Stop();
            looper.Begin();
        }

        void left_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (sumer > 0)
            {
                sumer -= 1;
            }
            showad(ads[sumer]);
            looper.Stop();
            looper.Begin();
        }

        void looper_Completed(object sender, EventArgs e)
        {
            if (sumer < counter)
            {
                sumer += 1;
                showad(ads[sumer]);
            }
            else if (sumer == counter) 
            {
                sumer = -1;  
            }
            looper.Begin();
        }

        int sumer = 0;
        int counter = 0;

        void beginadshow(List<ad> data)
        {
            counter = data.Count() - 1;
            showad(data[sumer]);
            looper.Begin();
        }

        Service1Client sc;

        void Page_Loaded(object sender, RoutedEventArgs e)
        {
            //loadserver
            sc = serverManager.getPox();
            //
            tbk_title.MouseLeftButtonDown += tbk_title_MouseLeftButtonDown;
            img1.MouseLeftButtonDown += tbk_title_MouseLeftButtonDown;
            video.MouseLeftButtonDown += tbk_title_MouseLeftButtonDown;
            sc.getByteCompleted += sc_getByteCompleted;
            sc.getsettingCompleted += sc_getsettingCompleted;
            //
            sc.getsettingAsync();
            sc.getADCompleted += sc_getADCompleted;
            sc.getADAsync();
        }

        void sc_getsettingCompleted(object sender, getsettingCompletedEventArgs e)
        {
            List<setting> set = new List<setting>(e.Result);
            this.Width = double.Parse(set.Single(d => d.set_name == "宽度").set_value);
            this.Height = double.Parse(set.Single(d => d.set_name == "高度").set_value);
            this.tbk_title.FontSize = double.Parse(set.Single(d => d.set_name == "字体大小").set_value);
            this.video.Volume = double.Parse(set.Single(d => d.set_name == "视频音量").set_value);
            clickmode = int.Parse(set.Single(d => d.set_name == "连接方式").set_value);
        }

        void sc_getADCompleted(object sender, getADCompletedEventArgs e)
        {
            ads = new List<ad>(e.Result);
            beginadshow(ads);
        }

        Uri clickurl;
        List<ad> ads;
        int clickmode = 1;

        void showad(ad d)
        {
            img1.ClearValue(Image.SourceProperty);
            tbk_title.Text = d.title;
            clickurl = new Uri(d.clickurl);
            adsort = d.sort;
            if (d.sort == 0)
            {
                video.Opacity = 0;
                video.Stop();
                sc.getByteAsync(d.img);
            }
            else if (d.sort == 1)
            {
                video.Opacity = 1;
                sc.getByteAsync(d.video);
            }
        }

        int adsort = 0;

        void sc_getByteCompleted(object sender, getByteCompletedEventArgs e)
        {
            BitmapImage bi = new BitmapImage();
            MemoryStream data = new MemoryStream(e.Result);

            if (adsort == 0)
            {
                bi.SetSource(data);
                img1.Source = bi;
            }
            else if (adsort == 1)
            {
                video.SetSource(data);
                video.Play();
            }
        }

        void tbk_title_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (clickmode == 1)
            {
                HtmlPage.Window.Navigate(clickurl, "blend");
            }
            else
            {
                HtmlPage.Window.Navigate(clickurl);
            }

        }
    }

}
